//
//  CAMyQRCodeViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/23.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAMyQRCodeViewController.h"
#import "CAQRCodeView.h"
#import "CAInviteCodeApi.h"
#import "UIImage+Extension.h"

@interface CAMyQRCodeViewController ()

@property (nonatomic, strong) CAQRCodeView *qrCodeView;

@end

@implementation CAMyQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self GetQRCode];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.qrCodeView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"我的二维码";
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_qrCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(35);
        make.top.equalTo(self.view).offset(70);
        make.right.equalTo(self.view).offset(-35);
        make.height.mas_equalTo(500);
    }];
}

- (CAQRCodeView *)qrCodeView {
    if (!_qrCodeView) {
        _qrCodeView = [CAQRCodeView new];
        _qrCodeView.backgroundColor = RGBCOLOR(241, 241, 241);
        _qrCodeView.layer.cornerRadius = 15;
    }
    return _qrCodeView;
}

#pragma mark private method
- (void) GetQRCode {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CAInviteCodeApi *inviteCodeApi = [[CAInviteCodeApi alloc] init];
    [inviteCodeApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAInviteCodeApi *api = (CAInviteCodeApi *) request;
        if (api.done) {
            self.qrCodeView.noLabel.text = [NSString stringWithFormat:@"（推荐码：%@）", api.responseObject[@"result"][@"invite_code"]];
            self.qrCodeView.nameLabel.text = api.responseObject[@"result"][@"user_name"];
            self.qrCodeView.qrCodeImageView.image = [UIImage createQRCodeWithSize:1024 content:api.responseObject[@"result"][@"invite_code"]];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

@end
